package com.boco.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.boco.entity.EMR_ZY_FYJS_FYLB;

public class GetEMR_ZY_FYJS_FYLBData {
	
	private static final Log log = LogFactory.getLog(GetEMR_ZY_FYJS_FYLBData.class);
	public static void getData()
	{
	    Connection con = null;
	    PreparedStatement pre = null;
	    ResultSet result = null;
	    
	    List<EMR_ZY_FYJS_FYLB> re = new ArrayList<EMR_ZY_FYJS_FYLB>();
	    try
	    {
	        Class.forName("oracle.jdbc.driver.OracleDriver");
	        System.out.println("开始尝试连接数据库！");
	        String url = "jdbc:oracle:" + "thin:@172.20.21.12:1521:orcl";
	        String urlsend = "http://fasong.cdxp.csb/json/dzbl4";
	        String user = "sjzx_hxk";
	        String password = "boco#1234";
	        
	        String userName = "dzbl4";
			String passwordsend = "322dfbf9048445cabb06cbf24e64cdcf";
			String resule = null;
			
	        con = DriverManager.getConnection(url, user, password);
	        System.out.println("连接成功！");
	        
	        String sql = "select "
	        		+"'' as YWID,"
					+"a.yljgdm as ZZJGDM,"
					+"'' as HZZSYH,"
					+"b.zyh as ZYH,"
					+"c.xmflbm as ZYFYLBDM,"
					+"c.xmflmc as ZYFYLBMC,"
					+"c.mxxmssje as FYJE,"
					+"to_char(a.ywscsj,'YYYY-MM-dd HH24:mi:ss') as BCRQ,"
					+"'' as ZRLSH,"
					+"'' as ORGCODE,"
					+"'' as AREACODE,"
					+"to_char(sysdate,'yyyyMMddHH24miss') as UPLOADTIME,"
					+"'' as  SOURCEID "
					+" from tb_zy_zyjsb a left join tb_zy_rydjb b "
					+" on a.yljgdm = b.yljgdm and a.kh = b.kh  "
					+" and a.klx = b.klx left join tb_zy_sfmxb c  "
					+" on a.yljgdm = c.yljgdm and a.kh = c.kh  "
					+" and a.klx = c.klx  ";
	 
	        log.info("查询sql："+sql);
	        pre = con.prepareStatement(sql);
	        result = pre.executeQuery();
	        System.out.println("执行成功");
	        while (result.next()){
	        	String param = null;
				param = "{\"SQL\":\"delete  EMR_ZY_FYJS_FYLB where ZZJGDM='"+
						result.getString("ZZJGDM")
						+"'"
						+" and YWID='"+result.getString("YWID") +"'"
						+" and ZYH='"+result.getString("ZYH") +"'"
						+ "\", \"ANNOTION\":\"cdxplogs\"}";
				resule = SentData.sendPost(urlsend, param, userName, passwordsend);
				log.info("删除结果："+resule);
				param = "{\"SQL\":\"INSERT INTO EMR_ZY_FYJS_FYLB ("
						+ "YWID,"
						+ "ZZJGDM,"
						+ "HZZSYH,"
						+ "ZYH,"
						+ "ZYFYLBDM,"
						+ "ZYFYLBMC,"
						+ "FYJE,"
						+ "BCRQ,"
						+ "ZRLSH,"
						+ "ORGCODE,"
						+ "AREACODE,"
						+ "UPLOADTIME,"
						+ "SOURCEID"
						+ ") VALUES("
						+ "'" + checkStr(result.getString("YWID")) + "'," 
						+ "'" + checkStr(result.getString("ZZJGDM")) + "'," 
						+ "'" + checkStr(result.getString("HZZSYH")) + "'," 
						+ "'" + checkStr(result.getString("ZYH")) + "'," 
						+ "'" + checkStr(result.getString("ZYFYLBDM")) + "'," 
						+ "'" + checkStr(result.getString("ZYFYLBMC")) + "'," 
						+ "" + checkStr(result.getString("FYJE")) + "," 

						+ "to_date('" + checkStr(result.getString("BCRQ")) + "','yyyy-MM-dd HH24:mi:ss'),"
						+ "'" + checkStr(result.getString("ZRLSH")) + "'," 
						+ "'" + checkStr(result.getString("ORGCODE")) + "'," 
						+ "'" + checkStr(result.getString("AREACODE")) + "'," 
						+ "sysdate," 
						+ "'dzbl4'" 
						+ ")\", \"ANNOTION\":\"cdxplogs\"}";
						resule = SentData.sendPost(urlsend, param, userName, passwordsend);
						log.info(resule);
	        }
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	    }
	    finally
	    {
	        try
	        {
	            if (result != null)
	                result.close();
	            if (pre != null)
	                pre.close();
	            if (con != null)
	                con.close();
	            System.out.println("数据库连接已关闭！");
	        }
	        catch (Exception e)
	        {
	            e.printStackTrace();
	        }
	    }
	}
	private static String checkStr(Object obj) {
		if (obj== null){
			obj = "";
		}
		return (String) obj;
	}
}
